package classifier;
import classifier.MaxEnt;
import entity.ClassifierResult;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by sunjia on 2018/1/23.
 */
public class mainTuTu {

    //组织训练数据
    public static void creatTrainData(String path_trainTxt, String path_traindata) throws IOException {
        // ================step1 组织训练数据成一个分词后TXT==============================
        System.out.println(path_trainTxt);
        OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(path_trainTxt,false),"UTF-8");
        File file = new File(path_traindata);
        File[] files = file.listFiles();// 获取目录下的所有文件或文件夹
        for (File f : files) {
            if (f.isFile()) {
                InputStreamReader inputStreamReader = new InputStreamReader(new FileInputStream(f), "utf-8");
                BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
                String label = bufferedReader.readLine();
                String line = "";
                while ((line = bufferedReader.readLine()) != null) {
                    writer.write(label + "\t" + line + "\n");
                }
            }
        }
        writer.close();
    }


    //数据预处理
    public static void main(String[] args) throws IOException {

//        String path_prop = MaxEnt.class.getClassLoader().getResource("classifier.prop").getPath();
//        String path_traindata = MaxEnt.class.getClassLoader().getResource("train").getPath();
//        String path_trainTxt = MaxEnt.class.getClassLoader().getResource("trainTxt").getPath();
//        String path_testdata = MaxEnt.class.getClassLoader().getResource("dev").getPath();
//        String path_model = MaxEnt.class.getClassLoader().getResource("model.bin").getPath();
//        String path_result = MaxEnt.class.getClassLoader().getResource("result").getPath();

        String path_prop = args[0];
        String path_traindata = args[1];
        String path_trainTxt = args[2];
        String path_testdata = args[3];
        String path_model = args[4];
        String path_result = args[5];

        //获取训练数据
        try {
            creatTrainData(path_trainTxt, path_traindata);
        } catch (IOException e) {
            e.printStackTrace();
        }

        //训练分类器
        MaxEnt maxEnt = new MaxEnt(path_prop, path_model);
        maxEnt.maxentTrain(path_trainTxt, path_model);

        //预测
        OutputStreamWriter writer1 = new OutputStreamWriter(new FileOutputStream(path_result,false),"UTF-8");

        File file = new File(path_testdata);
        File[] files = file.listFiles();// 获取目录下的所有文件或文件夹
        for (File f : files) {
            if (f.isFile()) {
                InputStreamReader inputStreamReader = new InputStreamReader(new FileInputStream(f), "utf-8");
                BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
                String label = bufferedReader.readLine();
                String line = "";
                while ((line = bufferedReader.readLine()) != null) {
                    List<ClassifierResult> classifierResults = maxEnt.maxentPredict1(label + "\t" + line);
                    double score = classifierResults.get(0).getScore();
                    String result = classifierResults.get(0).getAnswer();
                    if ( !(result.contains("诈骗") && score >0.9)) {
                        writer1.write(f.getName() + "\t" + result + "\t" + score + "\n");
                    }

                }
            }
        }
        writer1.close();
    }
}
